package com.neudu.glsb.mapper;


import com.neudu.glsb.pojo.EmployeeAttendance;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.neudu.glsb.dto.EmployeeAttendanceVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【employee_attendance(员工考勤信息)】的数据库操作Mapper
* @createDate 2025-04-16 22:28:32
* @Entity com.neudu.glsb.pojo.EmployeeAttendance
*/
@Mapper
public interface EmployeeAttendanceMapper extends BaseMapper<EmployeeAttendance> {
    @Select("SELECT COUNT(*) FROM employee_attendance WHERE employee_id = #{employeeId} AND is_late = 1")
    int getLateCountByEmployeeId(@Param("employeeId") Integer employeeId);
    @Select("SELECT ea.attendance_date as date, " +
            "COUNT(DISTINCT ea.employee_id) * 100.0 / (SELECT COUNT(*) FROM employees) as attendance_rate " +
            "FROM employee_attendance ea " +
            "GROUP BY ea.attendance_date")
    List<Map<String, Object>> getDailyAttendanceRate();
    /**
     * 查询用户当天是否已签到
     * @param employeeId 员工ID
     * @param today 当天日期（格式：yyyy-MM-dd）
     * @return 签到记录数（0=未签到，≥1=已签到）
     */
    @Select("SELECT COUNT(*) FROM employee_attendance " +
            "WHERE employee_id = #{employeeId} " +
            "AND DATE(attendance_date) = #{today}")
    int checkTodaySign(@Param("employeeId") Integer employeeId, @Param("today") String today);

    /**
     * 查询用户历史签到记录（按时间倒序）
     * @param employeeId 员工ID
     * @return 历史签到列表
     */
    @Select("SELECT id, attendance_date as attendanceDate, sign_in_time as signInTime, is_late as isLate " +
            "FROM employee_attendance " +
            "WHERE employee_id = #{employeeId} " +
            "ORDER BY attendance_date DESC")
    List<EmployeeAttendance> getHistorySign(@Param("employeeId") Integer employeeId);

    @Select("SELECT ea.id, ea.employee_id, ea.attendance_date, ea.is_signed, " +
            "       ea.sign_in_time, ea.is_late, e.employee_name " +
            "FROM employee_attendance ea " +
            "LEFT JOIN employees e ON ea.employee_id = e.id " +
            "ORDER BY ea.attendance_date DESC, ea.id DESC")
    IPage<EmployeeAttendanceVO> getAllAttendance(Page<EmployeeAttendanceVO> page);

}




